$(document).ready(addClickHandlers);

 function addClickHandlers() {
	$("a", this).click(function() {
		if (this.getAttribute('myFor')) {
			$(this.getAttribute('myFor')).load(this.href, addClickHandlers);
			activate(this);
			return false;
		}
	});

   $("form",this).submit(function() {
	if (this.getAttribute('myFor')) {
		$(this).ajaxSubmit({target: this.getAttribute('myFor'), success: addClickHandlers});
		return false;
	}
   });
}
 
 function reload(id,href) {
	$(id).load(href, addClickHandlers);
 }
 
 function activate(link) {
	$("a").each(function(pos) {
		if ($(this).hasClass("selected")) {
			$(this).removeClass("selected");
		 }
	});
	$(link).addClass("selected");
 }
 
 function setCheckButtons() {
       $("#orders td.orders a").click(function(event){
         if ($(this).hasClass("checked")) {
		$(this).removeClass("checked");
	 }
	 else {
		$(this).addClass("checked");
	 }
       });
}
function setNewordersButtons(){
       $("#allButton").click(function(){
                if ($(this).hasClass("checked")) {
			$("td.orders a").removeClass("checked");
			$(this).removeClass("checked");
		}
		else {
			$("td.orders a").addClass("checked")
			$(this).addClass("checked");
		}
		return false;
       });
       $("#printButton").click(function(){
		setCookie('selectedOrders',"");
		getSelected();
	        var selectedOrders=getCookie('selectedOrders');
		if (this.getAttribute('myFor')) $(this.getAttribute('myFor')).load(this.href, addClickHandlers);
		return false;
       });
       $("#processButton").click(function(){
		setCookie('selectedOrders',"");
		getSelected();
	        var selectedOrders=getCookie('selectedOrders');
		if (this.getAttribute('myFor')) $(this.getAttribute('myFor')).load(this.href, addClickHandlers);
		return false;
       });
}
function setPrintForeignButton(ids){
       $("#printForeignButton").click(function(){
		setCookie('selectedOrders',"");
		setCookie('selectedOrders',ids);
		if (this.getAttribute('myFor')) $(this.getAttribute('myFor')).load(this.href, addClickHandlers);
		return false;
       });
}
function getSelected() {
       $("td.orders a.checked").each(function(pos){
		var id;
		var list=this.href.slice(1);
		var pharams=list.split("&");
		for (var i=pharams.length-1;i>-1;i--) {
			var pharam=pharams[i].split("=");
				if (pharam[0]=="order_id") {
					id=pharam[1];
					i=-1;
				}
		}
		var cookie=getCookie('selectedOrders');
		if (cookie) {
			var selectedOrders=cookie.split(","); }
		else {
			var selectedOrders=new Array(); }
		selectedOrders.push(id);
		cookie=selectedOrders.join(",");
		setCookie('selectedOrders',cookie);		
       });
}

function printOrders(labels) {
	if (labels) {
		$("div.labels").printArea({mode: "popup", popClose: true});
	}
	$("div.PrintArea").each(function(pos) {
		$(this).printArea({mode: "popup", popClose: true});
	});
}

